﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class F510 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string v_str_donviID = Request.QueryString["donviId"];
        int v_int_donviID = 0;
        int.TryParse(v_str_donviID, out v_int_donviID);

        string v_str_nganhID = Request.QueryString["nganhId"];
        int v_int_nganhID = 0;
        int.TryParse(v_str_nganhID, out v_int_nganhID);
        if ((v_int_donviID != 0 && v_int_nganhID == 0)||(v_int_donviID == 0 && v_int_nganhID != 0)) Response.Redirect("F530.aspx");
        if (v_int_donviID == 0 && v_int_nganhID == 0) Response.Redirect("F500.aspx");

        if (!IsPostBack)
        {
            string v_str_gdID = Request.QueryString["Id"];
            int v_int_gdID = 0;
            int.TryParse(v_str_gdID, out v_int_gdID);
            if (v_int_gdID != 0)
            {
                this.m_cmd_tao_moi.Enabled = false;
                this.m_cmd_cap_nhat.Enabled = true;
            }
            else
            {
                this.m_cmd_tao_moi.Enabled = true;
                this.m_cmd_cap_nhat.Enabled = false;
            }

            LoadMonHoc();
            BindData();
        }
    }

    private void LoadMonHoc()
    {
        string v_str_donviID = Request.QueryString["donviId"];
        int v_int_donviID = 0;
        int.TryParse(v_str_donviID, out v_int_donviID);

        string v_str_nganhID = Request.QueryString["nganhId"];
        int v_int_nganhID = 0;
        int.TryParse(v_str_nganhID, out v_int_nganhID);

        string v_str_ID = Request.QueryString["Id"];
        int v_int_ID = 0;
        int.TryParse(v_str_ID, out v_int_ID);
        
        //GetcboMonHocByDonViNganhIdResult lst = Services.GetcboMonHocByDonViNganhId(v_int_donviID, v_int_nganhID);
        cboMonHoc.DataSource = Services.GetcboMonHocByDonViNganhId(v_int_donviID, v_int_nganhID);
        cboMonHoc.DataTextField = "TEN_TIENG_VIET";
        cboMonHoc.DataValueField = "ID";
        cboMonHoc.DataBind();

        if (v_int_ID != 0)
        {
            DM_DON_VI_NGANH_MON_HOC v_obj_DM_DON_VI_NGANH_MON_HOC = Services.GetDM_DON_VI_NGANH_MON_HOCById(v_int_ID);
            DM_MON_HOC monhoc = Services.GetDM_MON_HOCById((int)v_obj_DM_DON_VI_NGANH_MON_HOC.ID_MON_HOC);
            ListItem li = new ListItem();
            li.Value = monhoc.ID.ToString();
            li.Text = monhoc.TEN_TIENG_VIET;
            cboMonHoc.Items.Add(li);
        }
    }

    

    private void BindData()
    {
        try
        {
            DM_NGANH_DAO_TAO v_obj_DM_NGANH_DAO_TAO = new DM_NGANH_DAO_TAO();
            DM_DON_VI_SU_DUNG v_obj_DM_DON_VI_SU_DUNG = new DM_DON_VI_SU_DUNG();
            m_lbl_thong_bao.Text = "Không có thông báo gì.";

            string v_str_donviID = Request.QueryString["donviId"];
            int v_int_donviID = 0;
            int.TryParse(v_str_donviID, out v_int_donviID);

            string v_str_nganhID = Request.QueryString["nganhId"];
            int v_int_nganhID = 0;
            int.TryParse(v_str_nganhID, out v_int_nganhID);

            string v_str_ID = Request.QueryString["Id"];
            int v_int_ID = 0;
            int.TryParse(v_str_ID, out v_int_ID);
            if (v_int_ID != 0)
            {
                DM_DON_VI_NGANH_MON_HOC v_obj_DM_DON_VI_NGANH_MON_HOC = Services.GetDM_DON_VI_NGANH_MON_HOCById(v_int_ID);
                if (v_obj_DM_DON_VI_NGANH_MON_HOC != null)
                {
                    ListItem itemSelect = this.cboMonHoc.Items.FindByValue(v_obj_DM_DON_VI_NGANH_MON_HOC.ID_MON_HOC.ToString());
                    if (itemSelect != null)
                    {
                        int index = this.cboMonHoc.Items.IndexOf(itemSelect);
                        this.cboMonHoc.SelectedIndex = index;

                        this.txtMaMoi.Text = v_obj_DM_DON_VI_NGANH_MON_HOC.MA_MON_HOC_MOI;
                    }
                }
            }

            string v_str_info = "####";
            if ((v_int_donviID != 0) && (v_int_nganhID != 0) )
            {
                v_obj_DM_DON_VI_SU_DUNG = Services.GetDM_DON_VI_SU_DUNGById(v_int_donviID);
                v_obj_DM_NGANH_DAO_TAO = Services.GetDM_NGANH_DAO_TAOById(v_int_nganhID);
                v_str_info = (!String.IsNullOrEmpty(v_obj_DM_DON_VI_SU_DUNG.TEN_DON_VI) ?  v_obj_DM_DON_VI_SU_DUNG.TEN_DON_VI : "######") + (!String.IsNullOrEmpty(v_obj_DM_NGANH_DAO_TAO.TEN_NGANH) ? " -> " + v_obj_DM_NGANH_DAO_TAO.TEN_NGANH : "######");
                this.lblMonHocInfo.Text = v_str_info + " -> Danh sách môn học. "; 
            }

            this.lblF221.Text = v_str_info + " ->  F510 - Thêm mới môn học";

            List<GetMonHocResult> lst = Services.GetMonHoc(v_int_donviID, v_int_nganhID);

            if (lst != null)
            {
                gvDsMonHoc.DataSource = lst;
                gvDsMonHoc.DataBind();
                // Set event for Delete button
                foreach (GridViewRow row in gvDsMonHoc.Rows)
                {
                    ImageButton imageButton = (ImageButton)row.FindControl("imgBtDelete");
                    imageButton.Attributes.Add("OnClick", "javascript:return confirm('Bạn có chắc chắn muốn xóa môn học này?')");
                }
            }
            else
            {

            }

        }
        catch (Exception)
        {

        }

    }

    protected void gvDsMonHoc_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string v_str_donviID = Request.QueryString["donviId"];
        int v_int_donviID = 0;
        int.TryParse(v_str_donviID, out v_int_donviID);

        string v_str_nganhID = Request.QueryString["nganhId"];
        int v_int_nganhID = 0;
        int.TryParse(v_str_nganhID, out v_int_nganhID);


        int id = Convert.ToInt32(e.CommandArgument);

        switch (e.CommandName.ToUpper())
        {
            case "EDIT":
                Response.Redirect(String.Format("F510.aspx?donviId={0}&nganhId={1}&Id={2}", v_int_donviID, v_int_nganhID,id));
                break;
            case "DEL":
                bool isok = Services.DeleteDM_DON_VI_NGANH_MON_HOC(id);
                if (isok)
                {
                    Response.Redirect(String.Format("F510.aspx?donviId={0}&nganhId={1}", v_int_donviID, v_int_nganhID));
                }
                else
                {
                    this.m_lbl_mess.Text = "Lỗi, xóa không thành công!";
                    m_lbl_mess.Visible = true;
                }
                break;
        }
    }

    protected void m_cmd_tao_moi_Click(object sender, EventArgs e)
    {
        try
        {
            string v_str_donviID = Request.QueryString["donviId"];
            int v_int_donviID = 0;
            int.TryParse(v_str_donviID, out v_int_donviID);

            string v_str_nganhID = Request.QueryString["nganhId"];
            int v_int_nganhID = 0;
            int.TryParse(v_str_nganhID, out v_int_nganhID);

            DM_DON_VI_NGANH v_obj_dvn = Services.GetDM_DON_VI_NGANHByDonViIdNganhId(v_int_donviID, v_int_nganhID);

            string v_str_monhocID = cboMonHoc.SelectedValue;
            int v_int_monhocID = 0;
            int.TryParse(v_str_monhocID, out v_int_monhocID);


            DM_DON_VI_NGANH_MON_HOC entity = new DM_DON_VI_NGANH_MON_HOC();
            entity.ID_MON_HOC = v_int_monhocID;
            entity.ID_DON_VI_NGANH = v_obj_dvn.ID;
            if (String.IsNullOrEmpty(this.txtMaMoi.Text))
            {
                entity.MA_MON_HOC_MOI = Services.GetDM_MON_HOCById(v_int_monhocID).MA_MON_HOC;
            }
            else
            {
                entity.MA_MON_HOC_MOI = this.txtMaMoi.Text;
            }

            bool isOk = Services.InsertDM_DON_VI_NGANH_MON_HOC(entity);
            if (isOk)
            {
                Response.Redirect(Request.RawUrl.ToString());
            }
            else
            {
                this.m_lbl_mess.Text = "Đã có lỗi xảy ra khi thêm mới môn học.";
                this.m_lbl_mess.Visible = true;
            }
        }
        catch (Exception ex)
        {
            Logger.Instance.Write(ex);
        }

    }
    private void ClearForm()
    {
        
    }

    protected void m_cmd_cap_nhat_Click(object sender, EventArgs e)
    {
        string v_str_ID = Request.QueryString["Id"];
        int v_int_ID = 0;
        int.TryParse(v_str_ID, out v_int_ID);
        if (v_int_ID != 0)
        {
            try
            {
                string v_str_donviID = Request.QueryString["donviId"];
                int v_int_donviID = 0;
                int.TryParse(v_str_donviID, out v_int_donviID);

                string v_str_nganhID = Request.QueryString["nganhId"];
                int v_int_nganhID = 0;
                int.TryParse(v_str_nganhID, out v_int_nganhID);

                DM_DON_VI_NGANH v_obj_dvn = Services.GetDM_DON_VI_NGANHByDonViIdNganhId(v_int_donviID, v_int_nganhID);

                string v_str_monhocID = cboMonHoc.SelectedValue;
                int v_int_monhocID = 0;
                int.TryParse(v_str_monhocID, out v_int_monhocID);


                DM_DON_VI_NGANH_MON_HOC entity = new DM_DON_VI_NGANH_MON_HOC();
                entity.ID = v_int_ID;
                entity.ID_MON_HOC = v_int_monhocID;
                entity.ID_DON_VI_NGANH = v_obj_dvn.ID;
                if (String.IsNullOrEmpty(this.txtMaMoi.Text))
                {
                    entity.MA_MON_HOC_MOI = Services.GetDM_MON_HOCById(v_int_monhocID).MA_MON_HOC;
                }
                else
                {
                    entity.MA_MON_HOC_MOI = this.txtMaMoi.Text;
                }

                bool isOk = Services.UpdateDM_DON_VI_NGANH_MON_HOC(entity);
                if (isOk)
                {
                    Response.Redirect(Request.RawUrl.ToString());
                }
                else
                {
                    this.m_lbl_mess.Text = "Đã có lỗi xảy ra khi thêm mới môn học.";
                    this.m_lbl_mess.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.Write(ex);
            }
        }
        else
        {
            this.m_lbl_mess.Text = "Bạn hãy chọn version cần chỉnh sửa.";
        }
    }
    protected void gvDsMonHoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDsMonHoc.PageIndex = e.NewPageIndex;
        BindData();
    }
}